package com.tsd.core.utils;

import java.io.*;
import java.net.URLDecoder;
import java.util.*;

/**
 * @Description: 配置文件工具栏
 * 配置文件可以是.properties、.xml类型文件
 * 所有以XML结尾的方法是针对xml文件的，其他的是针对.properties文件的
 * @Author Hillpool
 * @Date 2019/3/11 16:08
 * @Modefied By:
 */
public class ConfigUtil {

    private static String classURL = ConfigUtil.class.getResource("/").getPath();
    final private static String filename = "config.properties";
    final private static String fileXML = "config.xml";

    /**
     * 将Map数据保存到classes/config.properties
     *
     * @param map
     * @throws Exception
     */
    public static void writePropertiesFile(Map<String, Object> map) throws Exception {
        if (classURL == null) {
            classURL = ConfigUtil.class.getResource("/").getPath();
        }
        String src = classURL + filename;
        Properties prop = new Properties();
        Map toSaveMap = new HashMap();
        FileInputStream input = null;
        try {
            input = new FileInputStream(src);
            prop.load(input);

            Set keys = prop.keySet();
            for (Iterator itr = keys.iterator(); itr.hasNext(); ) {
                String key = (String) itr.next();
                Object value = prop.get(key);
                toSaveMap.put(key, value);
            }

            toSaveMap.putAll(map);
            OutputStream outputStream = new FileOutputStream(src);
            prop.putAll(toSaveMap);
            prop.store(outputStream, "TSD系统配置文件");
            outputStream.close();
        } catch (Exception e) {
            throw e;
        } finally {
            FileUtil.safeCloseInputStrem(input);
        }
    }

    /**
     * 读取资源文件classes/config.properties读取数据
     *
     * @param key
     * @return
     */
    public static Object readPropertiesFile(String key) {
        if (classURL == null) {
            classURL = ConfigUtil.class.getResource("/").getPath();
        }
        String src = classURL + filename;
        Properties properties = new Properties();
        InputStream inputStream = null;
        try {
            inputStream = new FileInputStream(src);
            properties.load(inputStream);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            FileUtil.safeCloseInputStrem(inputStream);
        }
        Object value = properties.getProperty(key);
        return value;
    }

    /**
     * 从classes/config.xml文件读取参数
     */
    public static Object readPropertiesFileFromXML(String key) {
        if (classURL == null) {
            classURL = ConfigUtil.class.getResource("/").getPath();
        }
        String src = classURL + fileXML;
        Properties properties = new Properties();
        InputStream inputStream = null;
        try {
            inputStream = new FileInputStream(src);
            properties.loadFromXML(inputStream);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            FileUtil.safeCloseInputStrem(inputStream);
        }
        Object value = properties.getProperty(key);
        return value;
    }

    /**
     * 读取XML文件到Map中，返回的值中都去除前后空格
     *
     * @param fileName 文件名
     * @param related  是否相当地址（相对于class目录)
     * @param trim     是否需要将value中的前后空格去除
     * @return
     */

    public static Map<String, String> loadMapFromXML(String fileName, boolean related, boolean trim) {
        if (related) {
            if (classURL == null) {
                classURL = ConfigUtil.class.getResource("/").getPath();
            }
            fileName = classURL + fileName;
        }
        return loadMapFromXMLFullPath(fileName, trim);
    }

    /**
     * 读取XML文件到Map中
     *
     * @param filePath 文件路径
     * @param trim     是否去掉参数值的前后空格
     * @return
     */
    public static Map<String, String> loadMapFromXMLFullPath(String filePath, boolean trim) {
        Properties properties = new Properties();
        InputStream inputStream = null;
        Map<String, String> map = new LinkedHashMap<String, String>();
        try {
            if (FileUtil.isFileExist(filePath)) {
                try {
                    filePath = URLDecoder.decode(filePath, "UTF-8");
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            if (FileUtil.isFileExist(filePath)) {
                inputStream = new FileInputStream(filePath);
                properties.loadFromXML(inputStream);
                Set<Object> keys = properties.keySet();
                for (Object key : keys) {
                    String keyString = (String) key;
                    String value = (String) properties.get(keyString);
                    if (trim) {
                        map.put(keyString, value == null ? null : value.trim());
                    } else {
                        map.put(keyString, value);
                    }
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return map;
    }

    /**
     * 写资源文件到XML文件
     *
     * @param map
     * @throws Exception
     */
    public static void writePropertiesFileToXML(Map<String, Object> map) throws Exception {
        if (classURL == null) {
            classURL = ConfigUtil.class.getResource("/").getPath();
        }
        String src = classURL + fileXML;
        Properties properties = new Properties();
        try {
            InputStream inputStream = new FileInputStream(src);
            properties.loadFromXML(inputStream);
            inputStream.close();
            Map toSaveMap = new HashMap();
            Set keys = properties.keySet();
            for (Object o : keys) {
                String key = (String) o;
                Object value = properties.get(key);
                toSaveMap.put(key, value);
            }
            toSaveMap.putAll(map);
            OutputStream outputStream = new FileOutputStream(src);
            properties.putAll(toSaveMap);
            properties.storeToXML(outputStream, "TSD系统配置文件");
            outputStream.close();
        } catch (Exception e) {
            throw e;
        }
    }
}
